package model.dao;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import model.entity.Album;
import model.entity.User;

@Stateless
@Local(AlbumDaoLocal.class)
public class AlbumDaoBean extends GenericDaoBean<Album, Integer> implements
		AlbumDaoLocal {

	@Override
	public List<Album> findAlbumByUser(int userId) {
		try{
			Query q = em.createQuery("SELECT album FROM Album album WHERE album.user.id=:userid");
			q.setParameter("userid", userId);
			return  q.getResultList();
		}catch(NoResultException e){
			return null;
		}
	}

	@Override
	public boolean titleExists(String title) {
		try{
			Query q = em.createQuery("SELECT album FROM Album album WHERE album.title=:tit");
			q.setParameter("tit", title);
			Album album=(Album)q.getSingleResult();
			return  album==null?false:true;
		}catch(NoResultException e){
			return false;
		}
	}
}